﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Edc.MMAssemblyManager
{
    public class AssemblyMetadataManager
    {
        public void SaveMetadata(AssemblyMetadata metadata)
        {
            using(var file = new StreamWriter(GetPath(metadata.IssueId, metadata.AssemblyName)))
            {
                file.WriteLine(metadata.IssueId);
                file.WriteLine(metadata.AssemblyName);
                file.WriteLine(metadata.AssemblyVersion);
                file.Write(metadata.Comment);
            }
        }

        public AssemblyMetadata LoadMetadata(string filePath)
        {
            using (var file = new StreamReader(filePath))
            {
                return new AssemblyMetadata()
                {
                    IssueId = Int32.Parse(file.ReadLine()),
                    AssemblyName = file.ReadLine(),
                    AssemblyVersion = file.ReadLine(),
                    Comment = file.ReadToEnd()
                };
            }
        }

        public IList<AssemblyMetadata> GetIssueAssemblyMetadata(int issueId)
        {
            var metadata = new List<AssemblyMetadata>();
            foreach(var file in Directory.GetFiles(GetAppPath(), GetIssueFilePattern(issueId)))
            {
                metadata.Add(LoadMetadata(file));
            }
            return metadata;
        }

        public IList<int> GetPendingIssueIds()
        {
            var ids = new List<int>();
            foreach (var file in Directory.GetFiles(GetAppPath(), GetIssueFilePattern(null)))
            {
                int id = LoadMetadata(file).IssueId;
                if (!ids.Contains(id)) ids.Add(id);
            }
            return ids;
        }

        public void DeleteIssueAssemblyMetadata(int issueId)
        {
            foreach (var file in Directory.GetFiles(GetAppPath(), GetIssueFilePattern(issueId)))
            {
                File.Delete(file);
            }

            foreach (var file in GetIssueSqlScripts(issueId))
            {
                File.Delete(file);
            }
        }

        public IList<string> GetIssueSqlScripts(int issueId)
        {
            // A00_100430115338_domain_property_7879.sql
            return new List<string>(Directory.GetFiles(GetAppPath(), string.Format("*_{0}.sql", issueId)));
        }

        private string GetIssueFilePattern(int? issueId)
        {
            if (issueId != null)
            {
                return string.Format("{0}_*.txt", issueId);
            }
            else
            {
                return "*.txt";
            }
        }

        private string GetPath(int issueId, string assemblyName)
        {
            var fileName = string.Format("{0}_{1}.txt", issueId, assemblyName);
            return Path.Combine(GetAppPath(), fileName);
        }

        private string GetAppPath()
        {
            var appPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Tfs2Mantis");
            if (!Directory.Exists(appPath)) Directory.CreateDirectory(appPath);
            return appPath;
        }
    }
}
